package common

import (
	"fmt"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"path/filepath"
	"runtime"
)

func init() {
	pflag.Int64("port", 8080, "服务运行端口")
	pflag.String("env", "dev", "运行环境")
	pflag.Parse()
	viper.BindPFlags(pflag.CommandLine)
	viper.SetEnvPrefix("user_svc")
	viper.BindEnv("env")
	env := viper.Get("env")
	if env == "" {
		viper.Set("token_secret", "test")
		env = "dev"
	}
	//解决单元测试找配置文件失败
	_, f, _, _ := runtime.Caller(0)
	appPath, _ := filepath.Abs(filepath.Dir(filepath.Join(f, ".."+string(filepath.Separator))))
	configFilName := fmt.Sprintf("%s.config", env)
	viper.SetConfigName(configFilName)
	viper.SetConfigType("json")
	viper.AddConfigPath(".")
	viper.AddConfigPath("./config/")
	viper.AddConfigPath(appPath + "/config/")
	if err := viper.ReadInConfig(); err != nil {
		panic("配置文件未找到:" + configFilName)
	}
}
